#include <bits/stdc++.h>
using namespace std;

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while (ch < '0' or ch > '9') f |= (ch == '-'), ch = getchar();
	while (ch >= '0' and ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

int __stk[128], __top;
inline void write(int x) {
    if(x < 0) putchar('-'), x = -x;
	do { __stk[++__top] = x % 10, x /= 10; } while (x);
	while (__top) putchar(__stk[__top--] + '0');
}

const int mod = 998244353;

void Min(int &x, int y) { y < x and (x = y); }
void Max(int &x, int y) { y > x and (x = y); }

void inc(int &x, int y) { (x += y) >= mod and (x -= mod); }
void mul(int &x, int y) { x = 1ll * x * y % mod; }

int q_pow(int x, int k) { int res = 1; for (; k; k >>= 1, mul(x, x)) if (k & 1) mul(res, x); return res; }

bool stmer;

const int N = 8010;

int n, ans;
int h[N], L[N], R[N], a[N], s[N], tmp[N][N];

bool vis[N];

void dfs(int x, int w) {
    if (x > n or w > ans) return Min(ans, w);

    for (int i = 1; i <= n; i++) if (!vis[i]) {
        bool flag = 1; a[x] = i;
        for (int j = 1; j <= n; j++) s[j] = s[j - 1] + (!vis[j]);
        for (int j = 1; j < i and flag; j++) {
            if (vis[j] and L[j] and s[i - 1] - s[L[j]]) flag = 0;
        }
        for (int j = i + 1; j <= n and flag; j++) {
            if (vis[j] and R[j] and s[R[j]] - s[i]) flag = 0;
        }
        if (!flag) continue;
        vis[i] = 1;
        for (int j = 1; j < i; j++) {
            tmp[x][j] = R[j];
            if (vis[j] and (!R[j] or i > R[j])) R[j] = i;
        }
        for (int j = i + 1; j <= n; j++) {
            tmp[x][j] = L[j];
            if (vis[j] and (!L[j] or i < L[j])) L[j] = i;
        }
        dfs(x + 1, w + (i != h[x]));
        for (int j = 1; j < i; j++) R[j] = tmp[x][j];
        for (int j = i + 1; j <= n; j++) L[j] = tmp[x][j];
        vis[i] = 0;
    }
}

bool edmer;
signed main() {
	freopen("build.in", "r", stdin);
	freopen("build.out", "w", stdout);
	cerr << "[Memory] " << (&stmer - &edmer) / 1024 / 1024 << " MB\n";
	
    n = read(), ans = n;
    for (int i = 1; i <= n; i++) h[i] = read();

    dfs(1, 0);
    
    write(ans), putchar('\n');

    cerr << "[Runtime] " << (double) clock() / CLOCKS_PER_SEC << " seconds\n";
	return 0;
} 